package cc.sheji.controller.admin;
import cc.sheji.Service.PictureCollectionService;
import cc.sheji.domain.JsonResult;
import cc.sheji.domain.PictureCollection;
import cc.sheji.domain.PictureCollectionCriteria;
import cn.dev33.satoken.stp.StpUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

@Validated
@RestController
@RequestMapping(value = "/admin/v1/pictureCollection")
public class AdminPictureCollectionController {

    @Autowired
    PictureCollectionService pictureCollectionService;

    @RequestMapping("/{id}")
    public JsonResult get(@PathVariable("id") Long id) throws Exception {
        return JsonResult.Success(pictureCollectionService.get(id));
    }

    @PostMapping("")
    public JsonResult create(@RequestBody @Validated() PictureCollection entity) {
        entity.setId(null);

        return JsonResult.Success(pictureCollectionService.adminCreate(entity));
    }


    @PutMapping("/{id}")
    public JsonResult update(@PathVariable(name = "id") Long id, @RequestBody PictureCollection entity) {
        entity.setId(id);
        return JsonResult.Success(pictureCollectionService.update(entity));
    }

    @GetMapping("")
    public JsonResult list(PictureCollectionCriteria criteria, Pageable pageable) {
        return JsonResult.Success(pictureCollectionService.list(criteria, pageable));
    }
    @GetMapping("/mine")
    public JsonResult mineList(PictureCollectionCriteria criteria, Pageable pageable) {
        criteria.setUserId(StpUtil.getLoginIdAsLong());
        return JsonResult.Success(pictureCollectionService.mineList(criteria, pageable));
    }

    @DeleteMapping("/{id}")
    public JsonResult delete(@PathVariable("id") Long id) {
        return JsonResult.Success(pictureCollectionService.delete(id));
    }

}
